<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY dc "http://purl.org/dc/elements/1.1/">
  <!ENTITY dcterms "http://purl.org/dc/terms/">
  <!ENTITY foaf "http://xmlns.com/foaf/0.1/">
  <!ENTITY a "http://xmlns.heustics.org/schema/myelin/0.1/">
  <!ENTITY iso "http://xmlns.heustics.org/schema/iso/0.1/">
  <!ENTITY m "http://xmlns.heustics.org/ontology/myelin/0.1/">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exslt="http://exslt.org/common" 
  xmlns:str="http://exslt.org/strings" 
  xmlns:a="http://xmlns.heustics.org/schema/myelin/0.1/" 
  xmlns:l="http://xmlns.heustics.org/schema/i18n/0.1/"
  exclude-result-prefixes="xsl exslt str a l"
>    
  <xsl:key name="a:locale-select-input" match="*" use="*[1]"/>
  
  <xsl:template match="a:xlocale" mode="a:input-show" name="a:locale-select-input">  
    <xsl:param name="label"/>
    <xsl:param name="hint"/>
    <xsl:param name="lang-l10n" select="a:get-l10n('&iso;language')/l:context[@name='names']/*"/>
    <xsl:param name="region-l10n" select="a:get-l10n('&iso;region')/l:context[@name='names']/*"/>
    <div class='input text locale'>  
      <div class="key">
        <div>
          <xsl:call-template name="a:label-class-attr"/>
          <span>
            <xsl:value-of select="$label"/>
          </span>
          <div class="spacer"/>
        </div>        
        <div class='hint'>
          <xsl:if test="$hint">
            <span>
              <xsl:value-of select="$hint"/>
            </span>
          </xsl:if>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>      
      <div class='value'>        
        <select name="xlocale">   
          <xsl:variable name="locale">
            <xsl:for-each select="a:options/*">
              <locale>
                <xsl:if test="@selected">
                  <xsl:attribute name="selected">1</xsl:attribute>
                </xsl:if>
                <xsl:variable name="locale" select="str:split(@key,'-')"/>
                <lang key="{$locale[1]}">
                  <xsl:value-of select="$lang-l10n[@key=$locale[1]]"/>
                </lang>
                <region key="{$locale[2]}">
                  <xsl:value-of select="$region-l10n[@key=$locale[2]]"/>
                </region>
              </locale>
            </xsl:for-each>
          </xsl:variable>      
          <xsl:for-each select="exslt:node-set($locale)/*[generate-id()=generate-id(key('a:locale-select-input',*[1]))]">
            <xsl:sort select="string(*[1])"/>
            <xsl:variable name="lang" select="*[1]"/>
            <optgroup label="{$lang}">
              <xsl:for-each select="key('a:locale-select-input',$lang)">
                <xsl:sort select="string(*[2])"/>
                <xsl:variable name="region" select="*[2]"/>
                <option value="{concat($lang/@key,'-',$region/@key)}">
                  <xsl:if test="@selected">
                    <xsl:attribute name="selected">selected</xsl:attribute>
                  </xsl:if>
                  <xsl:value-of select="$region"/>
                </option>
              </xsl:for-each>
            </optgroup>
          </xsl:for-each>          
        </select>     
        <div class="spacer"/>   
      </div>
      <div class='spacer'/>
    </div>
  </xsl:template>
  
</xsl:stylesheet>

